﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Live;
using Clarity.Prototypes.Transitions;

namespace NotificationReceiver
{
    public partial class MainPage : PhoneApplicationPage
    {
        

        NotificationService.Service1Client svc = new NotificationService.Service1Client();
        AppSettings apps = new AppSettings();
        // Constructor
        public MainPage()
        {
            InitializeComponent();

        }
        
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            AppSettings apps = new AppSettings();
            if (apps.AunthicatedUserIdSetting != null && apps.AunthicatedUserIdSetting != "")
            {
                LoadNotification();
                LoadHistory();
            }
            else 
            {
                NavigationService.Navigate(new Uri("/SignIn.xaml", UriKind.Relative));
            }


        }

        void LoadNotification()
        {
            try
            {
                svc.GetLastNotificationUrlCompleted += new EventHandler<NotificationService.GetLastNotificationUrlCompletedEventArgs>(svc_GetLastNotificationUrlCompleted);
                svc.GetLastNotificationUrlAsync("admin_"+apps.AunthicatedUserIdSetting);
            }
            catch (Exception ex)
            {
                Exceptions.SaveException(ex.Message.ToString(), this.ApplicationTitle.ToString(), "admin");
                Exceptions.SaveException("Could not perform GetLastNotification operation in LoadNotification Method", this.ApplicationTitle.Text.ToString(), "admin");

            }
            finally
            {

            }

        }

        private void LoadHistory()
        {
            try
            {
                svc.GetAllNotificationsCompleted += new EventHandler<NotificationService.GetAllNotificationsCompletedEventArgs>(svc_GetAllNotificationsCompleted);
                svc.GetAllNotificationsAsync("admin_"+apps.AunthicatedUserIdSetting);
            }
            catch (Exception ex)
            {
                Exceptions.SaveException("Could not perform GetAllNotifications operation in LoadHistory Method", this.ApplicationTitle.Text.ToString(), "admin");
            }
            try
            {
                svc.SetStatusCompleted += new EventHandler<AsyncCompletedEventArgs>(svc_SetStatusCompleted);
                svc.SetStatusAsync("admin_"+apps.AunthicatedUserIdSetting);
            }
            catch (Exception ex)
            {
                Exceptions.SaveException("Could not perform SetStatus operation in LoadHistory Method", this.ApplicationTitle.Text.ToString(), "admin");
            }


        }

        void svc_GetLastNotificationUrlCompleted(object sender, NotificationService.GetLastNotificationUrlCompletedEventArgs e)
        {
            if (e.Result != null && e.Result != "")
            {
                DisplayNotication(e.type, e.Result);
            }


        }

        void svc_GetAllNotificationsCompleted(object sender, NotificationService.GetAllNotificationsCompletedEventArgs e)
        {
            try
            {
                ObservableCollection<Notification> notifications = new ObservableCollection<Notification>();
                IEnumerable<XElement> result = e.Result;
                if (e.Result != null)
                {
                    foreach (XElement ele in result)
                    {

                        Notification notification = new Notification();
                        notification.Type = ele.Element("type").Value;
                        notification.Message = ele.Element("message").Value;
                        if (notification.Type!="text")
                            notification.Displaymessage = System.IO.Path.GetFileName(ele.Element("message").Value);
                        else
                            notification.Displaymessage = notification.Message;
                        notifications.Add(notification);

                    }
                    lstnotifications.ItemsSource = notifications;

                }
                else
                {
                    Nodatatblock.Visibility = Visibility.Visible;
                }
            }

            catch (Exception ex)
            {
                Exceptions.SaveException("Could not perform operation in svc_GetAllNotificationsCompleted Method", this.ApplicationTitle.Text.ToString(), "admin");
            }
            finally { }
        }

        void DisplayNotication(string type, string result)
        {
            switch (type)
            {
                case "phone":
                    try
                    {
                        PhoneCallTask pct = new PhoneCallTask();
                        pct.PhoneNumber = result;
                        pct.Show();
                        SavePhoneNumberTask spnt = new SavePhoneNumberTask();
                        spnt.PhoneNumber = result;
                        spnt.Show();

                    }
                    catch (Exception ex)
                    {
                        Exceptions.SaveException("Could not perform Phone case operation in DisplayNotication Method", this.ApplicationTitle.Text.ToString(), "admin");

                    }
                    finally { }
                    break;

                case "text":
                    try
                    {
                        MessageBox.Show(result);
                    }

                    catch (Exception ex)
                    {
                        Exceptions.SaveException("Could not perform Text case operation in DisplayNotication Method", this.ApplicationTitle.Text.ToString(), "admin");

                    }
                    finally { }
                    break;
                default:
                    try
                    {
                        WebBrowserTask webtask = new WebBrowserTask();
                        webtask.URL = result;
                        webtask.Show();
                    }
                    catch (Exception ex)
                    {
                        Exceptions.SaveException("Could not perform default case operation in DisplayNotication Method", this.ApplicationTitle.Text.ToString(), "admin");

                    }
                    finally { }
                    break;
            }


        }

        void svc_SetStatusCompleted(object sender, AsyncCompletedEventArgs e)
        {


        }

        private void lstnotifications_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstnotifications.SelectedIndex != -1)
            {
                Notification noti = e.AddedItems[0] as Notification;

                switch (noti.Type)
                {
                    case "Phone":
                        DisplayNotication(noti.Type, noti.Message);
                        break;

                    default:
                        DisplayNotication(noti.Type, noti.Message);
                        break;
                }

            }
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                svc.DeleteHistoryAsync("admin_"+apps.AunthicatedUserIdSetting);
                svc.DeleteHistoryCompleted += new EventHandler<AsyncCompletedEventArgs>(svc_DeleteHistoryCompleted);
            }
            catch (Exception ex)
            {
                Exceptions.SaveException(ex.Message.ToString(), this.ApplicationTitle.ToString(), "admin");
            }
        }

        void svc_DeleteHistoryCompleted(object sender, AsyncCompletedEventArgs e)
        {
            lstnotifications.ItemsSource = null;
        }

        private void btntran_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Transition.xaml", UriKind.Relative));

        }
    }
    public class Notification
    {
        public Notification()
        { }

        private string type;

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        private string status;

        public string Status
        {
            get { return status; }
            set { status = value; }
        }

        private string displaymessage;

        public string Displaymessage
        {
            get { return displaymessage; }
            set { displaymessage = value; }
        }
    }
}